export const randomChoice = (arr, count) => {
  let retEles = []
  let selectedIndex = []
  while (retEles.length < count) {
    const randIndex = Math.floor(Math.random() * arr.length)
    if (selectedIndex.indexOf(randIndex) === -1) {
      retEles.push(arr[randIndex])
      selectedIndex.push(randIndex)
    }
    if (retEles.length === arr.length) break
  }
  return retEles
}

// 请求blob对象
export const GetBlob = (url) => {
  return new Promise((resolve) => {
    const xhr = new XMLHttpRequest()
    xhr.open('GET', url, true)
    xhr.responseType = 'blob'
    xhr.onload = () => {
      if (xhr.status === 200) {
        resolve(xhr.response)
      }
    }
    xhr.send()
  })
}

// 获取树形数据第一个节点
export const getFirstNodeOfTree = (treeList, childrenKey="children") => {
  for (let i=0; i < treeList.length; i++) {
    const childrenList = treeList[i][childrenKey]
    if (childrenList && childrenList.length > 0) {
      return getFirstNodeOfTree(childrenList, childrenKey)
    } else {
      return treeList[i]
    }
  }
}
